package com.tzz.controller;

import com.alibaba.fastjson2.JSONObject;
import com.tzz.config.CreateActivityConfig;
import com.tzz.entity.Product;
import com.tzz.vo.ActivityStationVo;
import com.tzz.entity.HttpResult;
import com.tzz.entity.PreActivity;
import com.tzz.exception.ActivityNotExistException;
import com.tzz.exception.StationNotInActivityException;
import com.tzz.service.PreActivityService;
import com.tzz.utils.MsgProduceUtil;
import com.tzz.utils.ModelDataUtil;
import com.tzz.vo.StationStop;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
//import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
//import org.springframework.messaging.handler.annotation.Header;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pre/activity")
public class ActivityController {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Autowired
    MsgProduceUtil producer;

    @Autowired
    PreActivityService preActivityService;

    @Autowired
    CreateActivityConfig createActivityConfig;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/createtest")
    public void create() throws IOException, MQClientException, MQBrokerException, RemotingException, InterruptedException {
        String class_path = this.getClass().getClassLoader().getResource("").getPath();
        String file_path = class_path+ "/data_model/create_activity.json";
        System.out.println(file_path);
        File file = new File(file_path);
        ObjectMapper objectMapper = new ObjectMapper();
        Map map = objectMapper.readValue(file,Map.class);
        System.out.println(map);
        JSONObject jsonObject = new JSONObject(map);
        System.out.println(jsonObject);
        JSONObject data_model = ModelDataUtil.getRequestDataModel("create_activity");
        System.out.println(data_model);
        System.out.println(data_model.get("productIds"));
        System.out.println(data_model.get("productIds").getClass());
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(2);
        System.out.println("更改键对应的值");
        jsonObject.put("stationOdoIds",integers);
        System.out.println(jsonObject);
        String dataModelStr = jsonObject.toJSONString(data_model);
        System.out.println("==============createActiityConfig==========================");
        System.out.println(createActivityConfig.getNameServer());
    }

    @PostMapping("/create")
    public HttpResult<PreActivity> timeTest(@RequestBody @Validated PreActivity preActivity){
        preActivityService.addActivity(preActivity);
        return HttpResult.success(preActivity);
    }

    @PostMapping("/stop")
    public <T> HttpResult<T> stopActivity(@RequestBody ActivityStationVo activityStationVo) throws StationNotInActivityException, ActivityNotExistException {
        preActivityService.stopActivity(activityStationVo.getPresaleId(),activityStationVo.getStationIds());
        return HttpResult.success();
    }

    @GetMapping("/query")
    public HttpResult<PreActivity> getActivity(String activityId){
        return HttpResult.success(preActivityService.findActivityById(activityId));
    }

    @PostMapping("query/all")
    public HttpResult<List<PreActivity>> getAllActivity(@RequestBody String activityId){
        return HttpResult.success(preActivityService.findAllActivity(String.valueOf(JSONObject.parse(activityId).get("activityId"))));
    }


    @GetMapping("/template")
    public void testTem(String presaleId){
        String testUrl = "http://localhost:8081/activity/query?activityId={presaleId}";
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("presaleId",presaleId);
        ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(testUrl, JSONObject.class, hashMap);
        System.out.println("==========================restTemplate--get==========================");
        System.out.println(responseEntity);
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getBody().getClass());

        System.out.println("==========================restTemplate--post==========================");
        String postUrl = "同步订单接口";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        JSONObject createOrder = ModelDataUtil.getRequestDataModel("create_order");
        HttpEntity<JSONObject> jsonObjectHttpHeadersHttpEntity = new HttpEntity<>(createOrder, httpHeaders);
        ResponseEntity<JSONObject> psotRes = restTemplate.postForEntity(postUrl, jsonObjectHttpHeadersHttpEntity, JSONObject.class);
        System.out.println(psotRes);
    }

    @PostMapping("/activity/station")
    public HttpResult<List<StationStop>> getStationStop(@RequestBody String activityId){
        return HttpResult.success(preActivityService.findStationStopMapByActivityId(String.valueOf(JSONObject.parse(activityId).get("activityId"))));
    }

    @PostMapping("/products")
    public HttpResult<List<Product>> getSkuIdsByPreId(@RequestBody String activityId){
        return HttpResult.success(preActivityService.findProductsByActivityId(String.valueOf(JSONObject.parse(activityId).get("activityId"))));
    }

}
